<template>
  <div>
    <!-- <van-nav-bar
      title="安福-App"
    /> -->
    <!-- <van-nav-bar
        :title="homeTitle"
      /> -->
    <div class="header">
      <img src="@/img/images/198.png" alt="" />
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="安福账号"
        placeholder="安福账号"
        :rules="[{ required: true, message: '请填写安福账号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link
      tag="div"
      :to="{ path: '/register' }"
      class="text-center text-gray-color"
      >没有账号？去注册</router-link
    >
    <hr />
    <div class="beianbox">
      <div class="beian">
        <img src="../img/images/警徽.png" alt="" />
      </div>
      <div class="beiantext">粤ICP备2021160051号</div>
    </div>
  </div>
</template>

<script>
import { fetchLogin } from "@/api/user.js";
export default {
  data() {
    return {
      username: "",
      password: "",
      homeTitle: "登录",
    };
  },
  methods: {
    async onSubmit(returnData) {
      // 请求登录接口
      let { status, message, token, userInfo } = await fetchLogin(returnData);
      this.$toast(message);
      if (status === 0) {
        //   this.$router.push('/home')
        //将token登录态存储到vuex,vuex再把数据同步到本地存储
        this.$store.commit("setToken", token);
        //保存用户的信息
        //登录后在users页面连接显示
        this.$store.commit("setUserInfo", userInfo);
        //重定向redirect
        let { redirect } = this.$route.query;
        if (redirect) {
          // 回到原来页面
          this.$router.push(redirect);
        } else {
          this.$router.push("/users");
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  justify-content: center;
}
.beianbox {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .beian img {
    width: 50px;
  }
}
</style>

